package com.xxx.dorm.web.servlet;

import com.xxx.dorm.domain.*;
import com.xxx.dorm.service.DormManagerService;
import com.xxx.dorm.service.UserService;
import com.xxx.dorm.service.impl.DormManagerServiceImpl;
import com.xxx.dorm.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/dormManager/*")
public class DormManagerServlet extends BaseServlet {

    private DormManagerService dormManagerService = new DormManagerServiceImpl();

    /**
     * 查找所有员工(宿管)信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String conditions = request.getParameter("conditions");
        String information = request.getParameter("information");
        List<Manage> manages = null;
        if(conditions != "" && information != "" && conditions != null && information != null){
            manages = dormManagerService.find(conditions,information);
        }else{
            manages = dormManagerService.findAll();
        }
        for(int i=0;i<manages.size();i++){
            List<Building> buildings = dormManagerService.findBuildBymid(manages.get(i).getId());
            manages.get(i).setBuildings(buildings);
        }
        request.setAttribute("manages",manages);
        request.setAttribute("mainRight","/WEB-INF/jsp/dorm/dormManagerList.jsp");
        request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
    }

    /**
     * 添加员工信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void addManager(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        String para = request.getParameter("para");
        if(para != null && "add".equals(para)){
            request.setAttribute( "mainRight","/WEB-INF/jsp/dorm/addDormManager.jsp");
            request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
            return;
        }

        Map<String, String[]> map = request.getParameterMap();
        //宿管管理的宿舍楼号
        String[] dormBuildIds = request.getParameterValues("dormBuildId");
        Manage manage = new Manage();
        BeanUtils.populate(manage,map);

        //完成保存宿管的操作并返回改宿管的id
        Integer managerId = dormManagerService.addManager(manage);
        //调用service保存中间表信息 t_manage_Building
        dormManagerService.addManagerToBuild(managerId,dormBuildIds);
        response.sendRedirect(request.getContextPath()+"/dormManager/findAll");
    }

    /**
     * 根据id查找员工
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findOneManager(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Manage manage = dormManagerService.findById(Integer.valueOf(id));
        //获取改宿管管理的宿舍楼
        List<Building> buildings = dormManagerService.findBuildBymid(manage.getId());
        manage.setBuildings(buildings);
        request.setAttribute("manage",manage);
        request.setAttribute( "mainRight","/WEB-INF/jsp/dorm/updateDormManager.jsp");
        request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
    }

    /**
     * 更新员工信息
     * @param request
     * @param response
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void updateManager (HttpServletRequest request, HttpServletResponse response) throws IOException, InvocationTargetException, IllegalAccessException {
        Map<String, String[]> map = request.getParameterMap();
        String[] dormBuildIds = request.getParameterValues("dormBuildId");
        Manage manage = new Manage();
        BeanUtils.populate(manage,map);
        //调用service完成更新
        dormManagerService.updateManager(manage,dormBuildIds);
        response.sendRedirect(request.getContextPath()+"/dormManager/findAll");
    }

    /**
     * 删除员工信息（包括中间件）
     * @param request
     * @param response
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void deleteManager (HttpServletRequest request, HttpServletResponse response) throws IOException, InvocationTargetException, IllegalAccessException {
        String id = request.getParameter("id");
        //调用service完成删除或者激活
        dormManagerService.deleteManager(id);
        response.sendRedirect(request.getContextPath()+"/dormManager/findAll");
    }
}
